import {RcFile} from "antd/es/upload";
import {message, Upload} from "antd";

// 图片转换为base64
export const getBase64 = (file: RcFile): Promise<string> =>
    new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result as string);
        reader.onerror = (error) => reject(error);
    });

// 数据获取为空，给提示
export const setNullData = (data: string | number, msg: string = '不详') => data ? data : msg;

//上传图片大小限制
export const beforeUpload = (file: RcFile) => {
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
        message.error('图片大小不能超出2m!');
    }
    return isLt2M || Upload.LIST_IGNORE;
};

